package com.toolkit.module.userInfo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.toolkit.common.Constants;
import com.toolkit.enumData.ExceptionCodeMsg;
import com.toolkit.exceptionHandler.BusinessException;
import com.toolkit.module.userInfo.bean.UserInfo;
import com.toolkit.module.userInfo.dto.UserLoginDto;
import com.toolkit.module.userInfo.mapper.UserInfoMapper;
import com.toolkit.module.userInfo.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toolkit.util.RedisUtil;
import com.toolkit.util.http.HttpUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author Maoco
 * @since 2025-08-05
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Value("${wx.appId}")
    private String wxAppId;

    @Value("${wx.appSecret}")
    private String wxAppSecret;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public UserLoginDto getAppToken(String wxCode) {
        //校验用户获取的微信code
        if(StringUtils.isEmpty(wxCode)){
            throw new BusinessException(ExceptionCodeMsg.TOKEN_VALID_CODE);
        }
        //获取微信的openId
        String param="appid="+wxAppId+"&secret="+wxAppSecret+"&js_code="+wxCode+"&grant_type=authorization_code";
        String json = HttpUtils.sendGet(Constants.GET_WX_OPEN_ID_URL,param,"utf-8");
        JSONObject jsonObject= JSON.parseObject(json);
        if(jsonObject ==null){
            throw new BusinessException(ExceptionCodeMsg.WX_PLAT_GET_DATA_ERROR_CODE);
        }
        String openId=jsonObject.getString("openid");
        if(StringUtils.isEmpty(openId)){
            throw new BusinessException(ExceptionCodeMsg.WX_PLAT_GET_OPENID_ERROR_CODE);
        }
        String sessionKey=jsonObject.getString("session_key");
        UserLoginDto userLoginDto=new UserLoginDto();
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("wx_open_id",openId);
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        String token = UUID.randomUUID().toString().replace("-", "");
        if(CollectionUtils.isEmpty(userInfoList)){
            UserInfo userInfo=new UserInfo();
            userInfo.setWxOpenId(openId);
            userInfoMapper.insert(userInfo);
        }
        userLoginDto.setToken(token);
        userLoginDto.setOpenId(openId);
        userLoginDto.setSessionKey(sessionKey);
        redisUtil.setCacheObject(token, JSON.toJSONString(userLoginDto), 30, TimeUnit.DAYS);
        return userLoginDto;
    }
}
